package com.lishe.lishe.lishe.module.mall.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.mall.activity.GoodsDetailActivity;
import com.lishe.lishe.lishe.module.mall.bean.MallGoods;

import java.util.LinkedList;

/**
 * Created by lwa on 2017/9/22.
 */

public class MallAllGoodsBaseAdapter extends RecyclerviewBaseAdapter {
    LinkedList<MallGoods> mallGoods;
    private Activity context;

    public MallAllGoodsBaseAdapter(Activity context, LinkedList<MallGoods> mallGoods) {
        this.mallGoods = mallGoods;
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mall_all, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, int position) {
        MallGoods mallGoodsObject = mallGoods.get(position);
        ImageLoaderUtils.displayImage(context,mallGoodsObject.getImage_default_id(), ((ViewHolder) holder).img,R.mipmap.defalut_img);
        //--------------------------------------滚动item---------------------------------------
        for (final MallGoods mallGoods : mallGoodsObject.getItems()) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_mall_scroll, null);
            ItemViewHolder itemViewHolder = new ItemViewHolder(view);
            ImageLoaderUtils.displayImage(context,mallGoods.getImage_default_id(), itemViewHolder.goods_img,R.mipmap.defalut_img);
            itemViewHolder.goods_name.setText(mallGoods.getTitle());
            itemViewHolder.integral.setText("¥" + mallGoods.getPrice());
            ((ViewHolder) holder).item_layout.addView(view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    GoodsDetailActivity.jump((Activity) context, mallGoods.getItem_id());
                }
            });
        }
    }

    @Override
    public int getItemCountMy() {
        return mallGoods == null ? 0 : mallGoods.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView img;
        private LinearLayout item_layout;
        public ViewHolder(View v) {
            super(v);
            img = v.findViewById(R.id.img);
            item_layout = v.findViewById(R.id.item_layout);
            item_layout.removeAllViews();
        }
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        //item
        private ImageView goods_img;
        private TextView goods_name;
        private TextView integral;

        public ItemViewHolder(View v) {
            super(v);
            goods_img = v.findViewById(R.id.goods_img);
            goods_name = v.findViewById(R.id.goods_name);
            integral = v.findViewById(R.id.integral);
        }
    }
}
